<?php

class CC_View_Helper_FolderList extends Zend_View_Helper_Abstract
{
    public function folderList($folderArray)
    {
        return $this->_generateFolderTreeView($folderArray);
    }
    
    private function _generateFolderTreeView($folderArray, $depth = 0)
    {
        $folderList = '';
        $depth++;
        
        if($depth == 1)
        {
            $folderList .= '<ul id="folderListTreeview" class="filetree">';
            $folderList .= '<li><span class="inbox"><a class="folderLink" href="0" ref="inbox">Inbox</a></span><ul>';
        }
            
        foreach($folderArray as $folder)
        {
            $tmpList = null;
            
            if(isset($folder['children']))
            {
                if(count($folder['children']))
                {
                    $tmpList .= '<ul>'.$this->_generateFolderTreeView($folder['children'], $depth).'</ul>';
                }
                
            }
            
            $folderList .= '<li'.($tmpList === null ? '' : '').'><span class="folder folderClosed"><a class="folderLink" href="#" ref="'.$folder['id'].'">'.$folder['name'].' '.($folder['ticketCount'] > 0 ? '('.$folder['ticketCount'].')':'').'</a></span>';
            
            if($tmpList !== null)
                $folderList .= $tmpList;
                
            $folderList .= '</li>';
        }
        
        if($depth == 1)
        {
            $folderList .= '';
            $folderList .= '</ul></li><li><span class="inbox"><a class="folderLink" href="/backoffice/helpdesk/mailbox/view/inbox">Outbox</a></span></li></ul>';
        }
        
        return $folderList;
    }
}
